using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace MKDS_Course_Editor.GCN.BMD
{
    class BMD
    {
        private static void readBmd(EndianBinaryReader er, BModel dst)
        {
            //skip file header
            er.BaseStream.Position = 0x20;

            UInt32 size = 0;
            string tag;
            int t;
            do
            {
                er.BaseStream.Seek(size, SeekOrigin.Current);
                t = (int)er.BaseStream.Length;

                tag = er.ReadString(Encoding.ASCII, 4);
                size = er.ReadUInt32();
                //Helper.toDWORD((uint)size);
                if (size < 8) size = 8; //prevent endless loop on corrupt data

                if (er.BaseStream.Position >= er.BaseStream.Length)
                {
                    break;
                }

                er.BaseStream.Position = t;

                //setStartupText("Parsing " + std::string(tag, 4) + "...");

                if (tag == "INF1")
                    INF1.dumpInf1(er, dst.inf1);
                else if (tag == "VTX1")
                    VTX1.dumpVtx1(er, dst.vtx1);
                else if (tag == "EVP1")
                    EVP1.dumpEvp1(er, dst.evp1);
                else if (tag == "DRW1")
                    DRW1.dumpDrw1(er, dst.drw1);
                else if (tag == "JNT1")
                    JNT1.dumpJnt1(er, dst.jnt1);
                else if (tag == "SHP1")
                    SHP1.dumpShp1(er, dst.shp1);
                //else if(tag == "MAT3")
                //else if (tag.Remove(2) == "MAT") //s_forest.bmd has a MAT2 section
                //    MAT.dumpMat3(er, dst.mat3);
                //else if (tag == "TEX1")
                //    TEX.dumpTex1(er, dst.tex1);
                else
                {
                    //warn("readBmd(): Unsupported section \'%c%c%c%c\'",
                    //  tag[0], tag[1], tag[2], tag[3]);
                }

                er.BaseStream.Position = t;

            } while (!(er.BaseStream.Position >= er.BaseStream.Length));
        }

        public static BModel loadBmd(string f)
        {
            EndianBinaryReader er = new EndianBinaryReader(File.OpenRead(f), Endianness.BigEndian);
            BModel ret = new BModel();
            readBmd(er, ret);
            return ret;
        }
        public class BModel
        {
            public INF1.Inf1 inf1 = new INF1.Inf1();
            public VTX1.Vtx1 vtx1 = new VTX1.Vtx1();
            public DRW1.Drw1 drw1 = new DRW1.Drw1();
            public EVP1.Evp1 evp1 = new EVP1.Evp1();
            public JNT1.Jnt1 jnt1 = new JNT1.Jnt1();
            public SHP1.Shp1 shp1 = new SHP1.Shp1();
            //public MAT.Mat3 mat3 = new MAT.Mat3();
            //public TEX.Tex1 tex1 = new TEX.Tex1();
        }


    }
}
